home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / edit / tde40.zip / port.c < prev    next >
C/C++ Source or Header  |  1994-06-05  |  18KB  |  625 lines

  1. /*
  2.  * Now that I own both MSC 7.0 and BC 3.1 and have linux, lets rearrange stuff
  3.  * so many compilers can compile TDE.  Several implementation specific
  4.  * functions needed for several environments were gathered into this file.
  5.  *
  6.  * In version 3.2, these functions changed to support unix.
  7.  *
  8.  * Incidentally, there is difference between a NULL line pointer and
  9.  * a pointer to a line that contains no characters.  For example, calling
  10.  *
  11.  *       line = malloc( 0 );
  12.  *
  13.  *                   or, more precisely in TDE:
  14.  *
  15.  *       line = _fmalloc( 0 );
  16.  *       line = farmalloc( 0 );
  17.  *
  18.  * will return a valid pointer to an item of 0 length in some compilers
  19.  * and a NULL pointer in other compilers.  malloc( 0 ) will return a valid
  20.  * pointer to an object of zero length in MSC.  malloc( 0 ) will return a
  21.  * NULL pointer in BC.  The problem with returning a NULL pointer for
  22.  * malloc( 0 ) is that it's a little harder to tell if the heap is out of
  23.  * memory or if we have a valid NULL pointer.  On the other hand, the good
  24.  * part about returning a NULL pointer for malloc( 0 ) is that extra space
  25.  * is not wasted for an object of 0 length.  In TDE, we will test for 0
  26.  * before calling my_malloc( ) and set an ERROR code if out of memory.
  27.  *
  28.  * Although many PC C compilers have findfirst and findnext functions for
  29.  * finding files, let's write our own to keep a closer watch on
  30.  * critical errors.
  31.  *
  32.  *
  33.  * New editor name:  TDE, the Thomson-Davis Editor.
  34.  * Author:           Frank Davis
  35.  * Date:             June 5, 1991, version 1.0
  36.  * Date:             July 29, 1991, version 1.1
  37.  * Date:             October 5, 1991, version 1.2
  38.  * Date:             January 20, 1992, version 1.3
  39.  * Date:             February 17, 1992, version 1.4
  40.  * Date:             April 1, 1992, version 1.5
  41.  * Date:             June 5, 1992, version 2.0
  42.  * Date:             October 31, 1992, version 2.1
  43.  * Date:             April 1, 1993, version 2.2
  44.  * Date:             June 5, 1993, version 3.0
  45.  * Date:             August 29, 1993, version 3.1
  46.  * Date:             November 13, 1993, version 3.2
  47.  * Date:             June 5, 1994, version 4.0
  48.  *
  49.  * This code is released into the public domain, Frank Davis.
  50.  * You may use and distribute it freely.
  51.  */
  52.  
  53. #include "tdestr.h"
  54. #include "common.h"
  55. #include "tdefunc.h"
  56. #include "define.h"
  57.  
  58. #if !defined( __UNIX__ )
  59.  #include <bios.h>       /* for REGS */
  60.  #include <dos.h>        /* for intdos */
  61. #endif
  62.  
  63. #if defined( __UNIX__ )
  64. /*
  65.  **********************************************************************
  66.  ******************************  PART 1  ******************************
  67.  **********************************************************************
  68.  *
  69.  * Let's try to make unix have the look and feel of a PC.
  70.  */
  71.  
  72. /*
  73.  * Name:    my_malloc
  74.  * Purpose: malloc from the far heap
  75.  * Date:    November 13, 1993
  76.  * Passed:  size:  memory needed heap
  77.  *          rc:   pointer to return code
  78.  * Notes:   set the return code only if an ERROR occured with malloc.
  79.  *           returning a NULL pointer is not neccessarily an ERROR.
  80.  */
  81. void *my_malloc( size_t size, int *rc )
  82. {
  83. void *mem;
  84.  
  85.    assert( size < MAX_LINE_LENGTH );
  86.  
  87.    if (size == 0)
  88.  
  89.       /*
  90.        * if 0 bytes are requested, return NULL
  91.        */
  92.       mem = NULL;
  93.    else {
  94.  
  95.       mem = malloc( size );
  96.  
  97.       /*
  98.        * if malloc failed, return NULL and an ERROR.
  99.        */
  100.       if (mem == NULL)
  101.          *rc = ERROR;
  102.    }
  103.    return( mem );
  104. }
  105.  
  106.  
  107. /*
  108.  * Name:    my_free
  109.  * Purpose: free memory from the far heap
  110.  * Date:    November 13, 1993
  111.  * Passed:  mem:  pointer to memory to free in far heap
  112.  */
  113. void my_free( void *mem )
  114. {
  115.    assert( mem != NULL );
  116.    free( mem );
  117. }
  118.  
  119.  
  120. /*
  121.  * Name:    my_heapavail
  122.  * Purpose: available free memory from the far heap
  123.  * Date:    June 5, 1994
  124.  * Notes:   use /proc/meminfo to find available free memory.  let's count
  125.  *            the free memory in the swap space(s), too.
  126.  */
  127. long my_heapavail( void )
  128. {
  129. FILE *f;                /* file pointer */
  130. char buf[82];           /* let's hope lines in /proc/meminfo are < 80 chars */
  131. char temp[82];          /* space to ascii free mem */
  132. char *p;                /* pointer walks down line in buf */
  133. char *q;                /* pointer for temp */
  134. long mem;               /* free memory */
  135. int  no;                /* current line number in /proc/meminfo */
  136.  
  137.    if ((f = fopen( "/proc/meminfo", "r" )) == NULL)
  138.       mem = 0;
  139.    else {
  140.       for (no=1,mem=0; !feof( f ); no++) {
  141.          fgets( buf, 80, f );
  142.          if (feof( f ))
  143.             break;
  144.          if (no >= 2) {
  145.  
  146.             /*
  147.              * assume line looks like:
  148.              *   "mem:   total used free buffers"
  149.              */
  150.             p = buf;
  151.             q = temp;
  152.             while (*p++ == ' ');
  153.             while (*p++ != ' ');
  154.             while (*p++ == ' ');
  155.             while (*p++ != ' ');
  156.             while (*p++ == ' ');
  157.             while (*p++ != ' ');
  158.             while (*p++ == ' ');
  159.             --p;
  160.             while ((*q++ = *p++) && *p != ' ');
  161.             *q = '\0';
  162.             mem += atol( temp );
  163.          }
  164.       }
  165.       fclose( f );
  166.    }
  167.    return( mem );
  168. }
  169.  
  170.  
  171. /*
  172.  * Name:    my_memcpy
  173.  * Purpose: copy memory
  174.  * Date:    November 13, 1993
  175.  * Passed:  dest: pointer to destination
  176.  *          src:  pointer to source
  177.  *          size: number of bytes to copy
  178.  */
  179. void my_memcpy( void *dest, void *src, size_t size )
  180. {
  181.    if (size > 0) {
  182.       assert( dest != NULL );
  183.       assert( src  != NULL );
  184.       memcpy( dest, src, size );
  185.    }
  186. }
  187.  
  188.  
  189. /*
  190.  * Name:    my_memmove
  191.  * Purpose: move memory
  192.  * Date:    November 13, 1993
  193.  * Passed:  dest: pointer to destination
  194.  *          src:  pointer to source
  195.  *          size: number of bytes to copy
  196.  */
  197. void my_memmove( void *dest, void *src, size_t size )
  198. {
  199.    if (size > 0) {
  200.       assert( dest != NULL );
  201.       assert( src  != NULL );
  202.       memmove( dest, src, size );
  203.    }
  204. }
  205.  
  206.  
  207. /*
  208.  * Name:    my_ltoa
  209.  * Purpose: ltoa is not ANSI - write our own
  210.  * Date:    November 13, 1993
  211.  * Passed:  lnum:   number to convert to ASCII.  in linux, an int is 32 bits.
  212.  *          s:      pointer to buffer
  213.  *          radix:  0 < radix <= 16
  214.  * Notes:   store the ascii string in a 20 character stack.
  215.  */
  216. char *my_ltoa( int lnum, char *s, int radix )
  217. {
  218. int  sign;
  219. char *digit = "0123456789abcdef";
  220. char stack[20];
  221. char *sp;
  222. char *p;
  223.  
  224.    if (radix < 0)
  225.       radix = -radix;
  226.  
  227.    /*
  228.     * default an empty string.
  229.     */
  230.    *s = '\0';
  231.    if (radix > 0 && radix <= 16) {
  232.       sign = 0;
  233.       if (lnum < 0) {
  234.          lnum = -lnum;
  235.          sign = -1;
  236.       }
  237.  
  238.       /*
  239.        * put a '\0' at the beginning of our stack.
  240.        *
  241.        * standard procedure: generate the digits in reverse order.
  242.        */
  243.       sp  = stack;
  244.       *sp = '\0';
  245.       do {
  246.          *++sp = digit[lnum % radix];
  247.          lnum = lnum / radix;
  248.       } while (lnum > 0);
  249.  
  250.       /*
  251.        * now, pop the ascii digits off the stack.  the '\0' that we stored
  252.        *  at the beginning of the stack terminates the string copy.
  253.        */
  254.       p = s;
  255.       if (sign == -1)
  256.          *p++ = '-';
  257.       while (*p++ = *sp--);
  258.    }
  259.    return( s );
  260. }
  261.  
  262.  
  263. /*
  264.  * Name:    my_findfirst
  265.  * Purpose: find the first file matching a pattern
  266.  * Date:    November 13, 1993
  267.  * Passed:  dta:    disk transfer address
  268.  *          path:   path to search for files
  269.  *          f_attr: attributes of files to search for
  270.  * Notes:   we don't use this function in a unix environment
  271.  */
  272. int  my_findfirst( DTA FAR *dta, char FAR *path, int f_attr )
  273. {
  274.    return( ERROR );
  275. }
  276.  
  277.  
  278. /*
  279.  * Name:    my_findnext
  280.  * Purpose: find the next file matching a pattern using POSIX readdir
  281.  * Date:    November 13, 1993
  282.  * Passed:  dp:  directory pointer (DIR is defined in <dirent.h>
  283.  *          unix_dta:  a pointer for TDE's dta
  284.  * Notes:   find directory elements.  the readdir( ) function gets the
  285.  *           next directory element.  it is up to TDE to figure the
  286.  *